import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Qt5Compat.GraphicalEffects
import FluentUI

Item {
    property var device
    FluClip {
        anchors.fill: parent
        radius: [4, 4, 4, 4]
        FluTableView {
            id: table_view
            anchors.fill: parent
            startRowIndex: 0

            // color: "#00000000"
            columnSource: [{
                    "title": "启用",
                    "dataIndex": 'enable',
                    "width"// frozen: false
                    : 50,
                    "frozen": true
                }, {
                    "title": "测量类型",
                    "dataIndex": 'type',
                    "width": 130,
                    "minimumWidth": 120
                }, {
                    "title": "激励电流",
                    "dataIndex": 'current',
                    "width": 110,
                    "minimumWidth": 100,
                    "customItem": com_current
                }, {
                    "title": "频率",
                    "dataIndex": 'freq',
                    "width": 130,
                    "minimumWidth": 120
                }, {
                    "title": "执行间隔",
                    "dataIndex": 'interval',
                    "width": 260,
                    "minimumWidth": 250
                }, {
                    "title": "优先级",
                    "dataIndex": 'priority',
                    "width": 120,
                    "minimumWidth": 110
                }]
        }

        Component {
            id: com_enable
            Item {
                FluCheckBox {
                    id: checkbox
                    anchors.centerIn: parent
                    checked: device.tasks[row].isEnabled
                    // checked: true === options.checked
                    // animationEnabled: false
                    // onCheckedChanged: {
                    //     console.log("Checkbox checked:", checked)
                    //     device.tasks[row].isEnabled = checked
                    // }
                    // clickListener: function () {
                    //     checked = !checked
                    //     device.tasks[row].isEnabled = checked
                    //     device.tasks[row].upload()
                    // }
                    onClicked: {
                        device.tasks[row].isEnabled = checked
                        device.tasks[row].upload()
                    }

                    Connections{
                        target: device.tasks[row]
                        function onIsEnabledChanged() {
                            if (!device.tasks[row].isEnabled) {
                                checkbox.checked = false
                            }
                        }
                    }
                }
            }
        }

        Component {
            id: com_type
            Item {
                FluComboBox {
                    anchors.centerIn: parent
                    width: 110
                    enabled: false
                    model: ListModel {
                        id: model
                        ListElement {
                            text: "电压刷新"
                        }
                        ListElement {
                            text: "单点测量"
                        }
                        ListElement {
                            text: "阻抗图谱"
                        }
                    }
                    currentIndex: device.tasks[row].taskType - 1
                }
            }
        }

        Component {
            id: com_current
            Item {
                FluComboBox {
                    anchors.centerIn: parent
                    width: 90
                    enabled: device.tasks[row].taskType !== 1
                    currentIndex: device.tasks[row].current
                    model: ListModel {
                        ListElement {
                            text: "0.15A"
                        }
                        ListElement {
                            text: "0.25A"
                        }
                        ListElement {
                            text: "0.5A"
                        }
                        ListElement {
                            text: "0.8A"
                        }
                        ListElement {
                            text: "1A"
                        }
                    }
                    onCommit: {

                    }
                }
            }
        }

        Component {
            id: com_freq
            Item {
                FluSpinBox {
                    anchors.centerIn: parent
                    enabled: device.tasks[row].taskType === 2
                    width: 110
                    value: device.tasks[row].freq
                    editable: true
                    from: 1
                    to: 2000
                    stepSize: 100
                    onValueModified: {
                        if (value < 1) {
                            value = 1
                        } else if (value > 2000) {
                            value = 2000
                        }
                        device.tasks[row].freq = value
                        if (device.tasks[row].isEnabled) {
                            device.tasks[row].isEnabled = false
                            device.tasks[row].upload()
                        }
                    }
                }
            }
        }

        Component {
            id: com_interval
            Item {
                RowLayout {
                    anchors.centerIn: parent
                    FluSpinBox {
                        id: spinbox_minute
                        Layout.preferredWidth: 100
                        // 从毫秒转换到分钟
                        value: Math.floor(device.tasks[row].interval / 60000)
                        editable: true
                        from: 0
                        to: 99
                        stepSize: 1
                        onValueModified: {
                            if (value < 0) {
                                value = 0
                            } else if (value > 99) {
                                value = 99
                            }
                            // 更新间隔时间
                            var interval = spinbox_minute.value * 60000 +
                                          spinbox_second.value * 1000
                            device.tasks[row].interval = interval
                            if (device.tasks[row].isEnabled) {
                                device.tasks[row].isEnabled = false
                                device.tasks[row].upload()
                            }
                        }
                    }
                    FluText {
                        Layout.alignment: Qt.AlignVCenter
                        text: "分"
                    }
                    FluSpinBox {
                        id: spinbox_second
                        // Layout.width: 6
                        Layout.preferredWidth: 100
                        // 从毫秒转换到秒
                        value: Math.floor(
                                   (device.tasks[row].interval % 60000) / 1000)
                        editable: true
                        from: 0
                        to: 59
                        stepSize: 1
                        onValueModified: {
                            if (value < 0) {
                                value = 0
                            } else if (value > 59) {
                                value = 59
                            }
                            // 更新间隔时间
                            var interval = spinbox_minute.value * 60000 +
                                          spinbox_second.value * 1000
                            device.tasks[row].interval = interval
                            if (device.tasks[row].isEnabled) {
                                device.tasks[row].isEnabled = false
                                device.tasks[row].upload()
                            }
                        }
                    }
                    FluText {
                        Layout.alignment: Qt.AlignVCenter
                        text: "秒"
                    }

                }
            }
        }

        Component {
            id: com_priority
            Item {
                FluSpinBox {
                    anchors.centerIn: parent
                    width: 100
                    value: 0
                    editable: true
                    from: 0
                    to: 9
                    stepSize: 1
                    onValueModified: {
                        if (value < 0) {
                            value = 0
                        } else if (value > 9) {
                            value = 9
                        }
                        device.tasks[row].priority = value
                        if (device.tasks[row].isEnabled) {
                            device.tasks[row].isEnabled = false
                            device.tasks[row].upload()
                        }
                    }
                }
            }
        }
    }

    onDeviceChanged: {
        table_view.sourceModel.clear()
        device.tasks.forEach(function (task) {
            table_view.appendRow({
                                     "enable": table_view.customItem(
                                                   com_enable),
                                     "type": table_view.customItem(com_type),
                                     "current": table_view.customItem(
                                                    com_current),
                                     "freq": table_view.customItem(com_freq),
                                     "interval": table_view.customItem(
                                                     com_interval),
                                     "priority": table_view.customItem(
                                                     com_priority)
                                 })
        })
    }
}
